package cn.kgc.controller.logic.system;


import cn.kgc.pojo.Jobtitle;
import cn.kgc.pojo.Quanxianinfo;
import cn.kgc.pojo.User;
import cn.kgc.pojo.Zhiweiquanxian;
import cn.kgc.service.system.smj_logService;
import cn.kgc.service.system.smj_zwqxService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.lang.reflect.Array;
import java.util.*;
import java.util.concurrent.ExecutionException;

@Controller
@RequestMapping("/zwqx")
public class smj_zwqxController {
    @Resource
    private smj_logService logService;


    @Resource
    public smj_zwqxService zwqx;

    /**
     * 查看全部职位
     *
     * @return
     */
    @RequestMapping("/zw")
    public ModelAndView getZWQXInfo() {
        ModelAndView mv = new ModelAndView();
        List<Jobtitle> jobtitles = zwqx.selQXM();
        mv.addObject("jobtitles", jobtitles);
        mv.setViewName("smj-ZhiWeiQuanXian");
        return mv;
    }


    /**
     * 分割字符串方法
     */
    public List splitStr(String str) {
        List list = new ArrayList();
        String[] split = str.split("-");

        for (int i = 0; i < split.length; i++) {
            if (split[i] != "0") {
                System.out.println("split[i] = " + split[i]);
                list.add(Integer.valueOf(split[i]));
            }
        }
        if (list.size() == 0) {
            list.add(-1);
        }
        return list;
    }


    /**
     * 查看权限信息
     *
     * @param
     * @return
     */
    @RequestMapping("/showqx2")
    @ResponseBody
    public String getZWQXInfoByNmae2(@Param("jb_id") Integer jb_id) {
        List<Quanxianinfo> a = zwqx.selQXPath();

        Zhiweiquanxian zw = zwqx.selJobQXByJbId(jb_id);
        List<Quanxianinfo> b = zwqx.selQXPathByAuths(splitStr(zw.getAuths()));

        List list = new ArrayList();
        list.add(a);
        list.add(b);
        System.out.println("b============ = " + b);
        return JSON.toJSONString(list);
    }

    /**
     * 编辑权限信息1
     *
     * @param
     * @return
     */
    @RequestMapping("/updQX")
    @ResponseBody
    public Boolean updQuanXian(@Param("data1") String data1, @Param("jb_id") Integer jb_id, HttpSession session) {
        User success = (User) session.getAttribute("success");
        try {
            logService.insertLog(success.getId(), "修改了职位（" + jb_id + ")的权限");
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("data1 = " + data1 + "jb_id=" + jb_id);
        int i = zwqx.updQXByJbId(data1, jb_id);
        if (i > 0) {
            return true;
        }
        return false;
    }


    /**
     * 查看权限信息
     *
     * @return
     */
    @RequestMapping("/zw2")
    public ModelAndView getZWQXInfo2(Integer id, Integer currentPage, Integer pageSize) {

        ModelAndView mv = new ModelAndView();
        if (currentPage == null || pageSize == null) {
            currentPage = 1;
            pageSize = 10;
        }

        PageHelper.startPage(currentPage, pageSize);

        Page<Quanxianinfo> quanxianinfos = zwqx.selQuanxianinfoById(id);

        PageInfo pageInfo = new PageInfo(quanxianinfos);

        System.out.println("pageInfo ============================================= " + pageInfo);
        mv.addObject("quanxianinfos", pageInfo);
        mv.setViewName("/smj-ZhiWeiQuanXian2");
        return mv;
    }


    /**
     * 修改权限信息2
     */
    @RequestMapping("/upinfo2")
    @ResponseBody
    public Boolean upinfo2(Quanxianinfo quanxianinfo, HttpSession session) {
        User success = (User) session.getAttribute("success");
        try {
            logService.insertLog(success.getId(), "修改了页面（" + quanxianinfo.getAuth() + ")的路径");
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        boolean b = zwqx.updateZwqxInfoById(quanxianinfo);
        return b;
    }

    /**
     * 增加权限信息
     */
    @RequestMapping("/addquanxian")
    @ResponseBody
    public Boolean addquanxian(Quanxianinfo quanxianinfo,HttpSession session) {
        User success = (User) session.getAttribute("success");
        try {
            logService.insertLog(success.getId(), "新增页面路径");
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        boolean b = zwqx.insertZwQxInfo(quanxianinfo);
        return b;
    }
    /**
     * 删除权限信息
     */
    @RequestMapping("/delquanxian")
    @ResponseBody
    public Boolean delquanxian(Integer id,HttpSession session) {
        User success = (User) session.getAttribute("success");
        try {
            logService.insertLog(success.getId(), "删除页面路径");
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        boolean b = zwqx.delZwQxInfo(id);
        return b;
    }

}






